from os import popen
import re

PING_RESULT = re.compile(r'^Host:\s+(.*)\s+\((.*)\)\s+Status:\s+(.*)$', flags=re.MULTILINE)

def do_ping(host):
    
    # Create command
    cmd = "/usr/local/bin/nmap -v -n -sP -oG - %s"%(host.address,)

    # Do ping
    fd = popen(cmd)

    # Read result
    for line in fd:
        if line[0]=="#":
            continue
        
        res = PING_RESULT.search(line)
        if res:
            ip, name, state = res.groups()
            if state == 'Up' and ip==host.address:
                state = True
            else:
                state = False
            
            return state

    return None
